package org.simplextensions.ui.settings;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.LayoutStyle;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;

import org.simplextensions.registry.IExtensionRegistry;
import org.simplextensions.ui.TreeModelSupport;

//VS4E -- DO NOT REMOVE THIS LINE!

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class SettingsDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel buttonsPanel;
	private JButton jButton1;
	private JTree editorsTree;
	private JPanel editorParent;
	private JPanel treeParent;
	private JSplitPane jSplitPane1;
	private JButton jButton2;
	private JSeparator jSeparator1;
	private final IExtensionRegistry extensionRegistry;

	public SettingsDialog(IExtensionRegistry extensionRegistry) {
		this(extensionRegistry, null);
	}

	public SettingsDialog(IExtensionRegistry extensionRegistry, Frame parent) {
		super(parent);
		this.extensionRegistry = extensionRegistry;
		initComponents();
	}

	private void initComponents() {
		setFont(new Font("Dialog", Font.PLAIN, 12));
		GroupLayout thisLayout = new GroupLayout((JComponent) getContentPane());
		getContentPane().setLayout(thisLayout);
		{
			buttonsPanel = new JPanel();
			GroupLayout jPanel1Layout1 = new GroupLayout((JComponent) buttonsPanel);
			buttonsPanel.setLayout(jPanel1Layout1);
			{
				jButton1 = new JButton();
				jButton1.setText("jButton1");
			}
			{
				jButton2 = new JButton();
				jButton2.setText("jButton2");
			}
			jPanel1Layout1.setHorizontalGroup(jPanel1Layout1.createSequentialGroup().addContainerGap(462, 462)
					.addComponent(jButton1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(jButton2, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
			jPanel1Layout1.setVerticalGroup(jPanel1Layout1
					.createSequentialGroup()
					.addContainerGap()
					.addGroup(
							jPanel1Layout1
									.createParallelGroup(GroupLayout.Alignment.BASELINE)
									.addComponent(jButton1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
									.addComponent(jButton2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE,
											GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)).addContainerGap(15, 15));
		}
		{
			jSplitPane1 = new JSplitPane();
			jSplitPane1.setDividerSize(8);
			{
				treeParent = new JPanel();
				treeParent.setBackground(Color.WHITE);
				GroupLayout treeParentLayout = new GroupLayout((JComponent) treeParent);
				treeParent.setPreferredSize(new java.awt.Dimension(180, 389));
				treeParent.setLayout(treeParentLayout);
				{
					editorsTree = new JTree(new TreeModelSupport() {

						SettingsExtensionPoint sep = extensionRegistry.getExtensionPoint(SettingsExtensionPoint.class);

						private List<SettingsEditorMetaData> topElements;

						private List<SettingsEditorMetaData> getTopElements() {
							if (topElements == null) {
								topElements = extensionRegistry.getExtensionPoint(SettingsExtensionPoint.class).getSettingsEditors();
							}
							return topElements;
						}

						public Object getChild(Object parent, int index) {
							if (parent == sep) {
								return getTopElements().get(index);
							} else if (parent instanceof SettingsEditorMetaData) {
								return ((SettingsEditorMetaData) parent).getChildren().get(index);
							}
							return null;
						}

						public int getChildCount(Object parent) {
							if (parent == sep) {
								return getTopElements().size();
							} else if (parent instanceof SettingsEditorMetaData) {
								return ((SettingsEditorMetaData) parent).getChildren().size();
							} else
								return 0;
						}

						public int getIndexOfChild(Object parent, Object child) {
							if (parent == sep) {
								return getTopElements().indexOf(child);
							} else if (parent instanceof SettingsEditorMetaData) {
								return ((SettingsEditorMetaData) parent).getChildren().indexOf(child);
							} else
								return 0;
						}

						public Object getRoot() {
							return sep;
						}

						public boolean isLeaf(Object node) {
							if (node instanceof SettingsEditorMetaData) {
								return ((SettingsEditorMetaData) node).getChildren().size() == 0;
							}
							return false;
						}

						public void valueForPathChanged(TreePath path, Object newValue) {
						}
					});
					editorsTree.setBounds(0, 0, 95, 76);
					editorsTree.setRootVisible(false);
					
					DefaultTreeCellRenderer renderer2 = new DefaultTreeCellRenderer();
				    renderer2.setOpenIcon(null);
				    renderer2.setClosedIcon(null);
				    renderer2.setLeafIcon(null);
				    editorsTree.setCellRenderer(renderer2);
				    editorsTree.setShowsRootHandles(true);
				}
				treeParentLayout.setHorizontalGroup(treeParentLayout.createSequentialGroup().addGap(4)
						.addComponent(editorsTree, 0, 130, Short.MAX_VALUE).addGap(4));
				treeParentLayout.setVerticalGroup(treeParentLayout.createSequentialGroup().addGap(4)
						.addComponent(editorsTree, 0, 96, Short.MAX_VALUE).addGap(4));
			}
			jSplitPane1.add(treeParent, JSplitPane.LEFT);
			{
				editorParent = new JPanel();
			}
			jSplitPane1.add(editorParent, JSplitPane.RIGHT);
		}
		{
			jSeparator1 = new JSeparator();
		}
		thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addComponent(jSplitPane1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)
				.addComponent(jSeparator1, 2, 2, 2).addComponent(buttonsPanel, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE));
		thisLayout.setHorizontalGroup(thisLayout.createParallelGroup()
				.addComponent(jSeparator1, GroupLayout.Alignment.LEADING, 0, 624, Short.MAX_VALUE)
				.addComponent(buttonsPanel, GroupLayout.Alignment.LEADING, 0, 624, Short.MAX_VALUE)
				.addComponent(jSplitPane1, GroupLayout.Alignment.LEADING, 0, 624, Short.MAX_VALUE));
		setSize(640, 480);
	}
}
